package photostock.model;

import java.util.Vector;

public class User extends UserItem {

	private static final long serialVersionUID = 20090501L;
	
	private boolean isLogged = false;
	private String login;
	
	public User (String s) {
		super(s);
		initParentItem(this);
		neededFields = "type,profil,acceptMail,mailAdresse";
		childrenFields = "id,nom,theme,skin,creationDate,user";
		children = new Vector<UserItem>();
		login = s;
	}

	private boolean initItem() {
		infos = super.initItem(this);
		return infos.exists();
	}

	public boolean login(String password) {
		query = "SELECT * FROM user WHERE login='"+this.login+"' and password='"+byteToHex(password.getBytes())+"'";
		isLogged = initItem();
		return isLogged;
	}

	public boolean register(String password, boolean acceptMail, String mailAdresse) {
		query = "INSERT INTO user values('"+login+"', '" +byteToHex(password.getBytes()) + "', 'inscrit', 'new', " + acceptMail + ", '"+ mailAdresse + "')";
		if (super.recItem(this)) 
			isLogged = login(password);
		return isLogged;
	}
	
	public boolean update() {
		query = "UPDATE user SET type='" + getType() + "', profil='" + getProfil() + "', acceptMail="+ acceptMail() +", mailAdresse='" + getMailAdresse() + "' where login='" + login + "'";
		return super.updateItem(this);
	}
	
	public boolean delete() {
		query = "DELETE FROM user WHERE login='" + login + "'";
		return super.deleteItem(this);
	}
	
	public boolean initChildrenItems() {
		query = "SELECT * FROM album WHERE user='" + login + "' order by id";
		for (ItemInfo infos : super.getChildrenInfos()) {
			Album child = new Album(this, infos.get("nom"));
			child.infos = infos;
			add(child);
		}
		return children.size() == 0 ? false : true;
	}
	
	public boolean createAlbum(String s) {
		Album al = new Album(this, s);
		al.createItem();
		add(al);
		return al.created();
	}
	
	public void disconnect() {
		infos = null;
		isLogged = false;
	}
	
	public boolean isLogged() {
		return isLogged;
	}
	
	public String getLogin() {
		return login;
	}
		
	public String getType() {
		return (null == infos || !infos.exists() || null == infos.get("type") ? "visiteur" : infos.get("type"));
	}

	public void setType(String s) {
		if (null != infos && infos.exists() && null != infos.get("type")) infos.set("type", s);
	}
	
	public boolean acceptMail() {
		return (null == infos || !infos.exists() || null == infos.get("acceptMail") ? false : new Boolean(infos.get("acceptMail")));
	}

	public void acceptMail(boolean b) {
		if (null != infos && infos.exists() && null != infos.get("acceptMail")) infos.set("acceptMail", new Boolean(b).toString());
	}

	public String getMailAdresse() {
		return (null == infos || !infos.exists() || null == infos.get("mailAdresse") ? "" : infos.get("mailAdresse"));
	}

	public void setMailAdresse(String s) {
		if (null != infos && infos.exists() && null != infos.get("mailAdresse")) infos.set("mailAdresse", s);
	}
		
	public String getProfil() {
		return (null == infos || !infos.exists() || null == infos.get("profil") ? "" : infos.get("profil"));
	}

	public void setProfil(String s) {
		if (null != infos && infos.exists() && null != infos.get("profil")) infos.set("profil", s);
	}
	
    public static String byteToHex(byte[] bits) {
        if (bits == null) {
            return null;
        }
        StringBuffer hex = new StringBuffer(bits.length * 2); // encod(1_bit) => 2 digits
        for (int i = 0; i < bits.length; i++) {
            if (((int) bits[i] & 0xff) < 0x10) { // 0 < .. < 9
                hex.append("0");
            }
            hex.append(Integer.toString((int) bits[i] & 0xff, 16)); // [(bit+256)%256]^16
        }
        return hex.toString();
    }
 
    public static byte[] hexToByte(String bits) {
        if ((bits == null) || (bits.length() % 2 != 0)) { // pair [xy]
            return null;
        }
        byte[] bytes = new byte[bits.length() / 2]; // decod(2_digits) => 1 bit
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte) Integer.parseInt(bits.substring(2 * i, 2 * i + 2), 16); // |bit|^16
        }
        return bytes;
    }
}
